Skip to content

fix(sse): support streaming responses on Fiber/fasthttp adapters#1059

Merged
wolveix merged 2 commits into
mainfrom
fix/sse-fiber-streaming
Jul 11, 2026
Merged

fix(sse): support streaming responses on Fiber/fasthttp adapters#1059
wolveix merged 2 commits into
mainfrom
fix/sse-fiber-streaming

Conversation

@wolveix

@wolveix wolveix commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Fixes #888.

Server-Sent Events (and other streaming responses) fail on the Fiber adapters with unable to flush. Fiber is the only adapter built on fasthttp rather than net/http, and fasthttp.RequestCtx doesn't implement http.Flusher — it can't flush the response body synchronously from within the handler, only via the deferred SetBodyStreamWriter callback. Every other adapter (chi, echo, gin, go, httprouter, mux, bunrouter, flow) exposes an http.Flusher and already works.

Approach

An internal, unexported streaming hook — no new public API:

  • The sse package detects an optional bodyStreamer interface on the context, alongside the existing unwrapper/writeDeadliner capability interfaces. When present, it streams through the adapter's callback; otherwise it writes to BodyWriter directly as before.
  • The Fiber v2 and v3 adapters implement it via fasthttp's SetBodyStreamWriter, passing a small writer that satisfies io.Writer, http.Flusher, and the write-deadline interface, so the shared send loop treats it uniformly.
  • fasthttp stays an indirect dependency — it is not imported by the sse package or the humafiber source, so no SSE user (chi/net-http/etc.) picks up a new direct dependency.
  • The SSE send loop is extracted into a shared stream() function reused by both paths.

Tests

  • humafiber.TestSSE (v3) and TestSSEV2 (v2) drive SSE end-to-end through the real fasthttp stream path and assert the exact streamed output.
  • The existing net/http SSE tests are unchanged and still pass, confirming no regression on the direct-write path.

Credit

Thanks to @zhouyusd for surfacing this and the first attempt in #1014, and to the reporters on #888. This PR takes a different approach that keeps fasthttp out of the core dependency graph, so it supersedes #1014.

SSE (and other streaming responses) failed on the Fiber adapters with
"unable to flush", because Fiber is built on fasthttp, whose RequestCtx
does not implement http.Flusher and can't flush the response body
synchronously from within the handler; it only streams via
SetBodyStreamWriter.

Add an internal, unexported streaming hook: the sse package detects an
optional bodyStreamer interface on the context (alongside the existing
unwrapper/writeDeadliner) and, when present, streams through the adapter's
callback. The Fiber v2 and v3 adapters implement it via fasthttp's
SetBodyStreamWriter, passing a small writer that satisfies io.Writer,
http.Flusher, and the write-deadline interface so the shared send loop
treats it uniformly. fasthttp stays an indirect dependency and is not
imported by the sse package or the humafiber source.

The SSE send loop is extracted into a shared stream() function reused by
both the direct-write and streaming paths.

Fixes #888.
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.13%. Comparing base (6cc787b) to head (b0d7bbb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1059   +/-   ##
=======================================
  Coverage   93.12%   93.13%           
=======================================
  Files          23       23           
  Lines        4932     4938    +6     
=======================================
+ Hits         4593     4599    +6     
  Misses        275      275           
  Partials       64       64           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add a white-box test that drives Register through a fake huma.Adapter whose
context implements bodyStreamer, exercising the Fiber/fasthttp streaming
branch (which codecov can't credit from the adapter tests, since adapters are
excluded and coverage is measured per package).
@wolveix wolveix merged commit a8a668c into main Jul 11, 2026
7 checks passed
@wolveix wolveix deleted the fix/sse-fiber-streaming branch July 11, 2026 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSE: deadline failed to flush

1 participant